/*
 * Copyright 2009 Jesse McLaughlin (nzjess@gmail.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.travelfusion.xmlclient.impl.login;

import org.travelfusion.xmlclient.login.Login;
import org.travelfusion.xmlclient.login.LoginManager;
import org.travelfusion.xmlclient.xobject.XRequest;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

/**
 * Simple implementation of {@link LoginManager}, which just returns a user-supplied {@link Login} instance. That is,
 * the login details do not depend on the request being handled.
 * <p>
 * This should be sufficient for most applications, since in general a given application will have a single unique login
 * id (obtained as part of the process of registering as an affiliate with TRAVELfusion).
 * 
 * @author Jesse McLaughlin (nzjess@gmail.com)
 */
@Singleton
public class SimpleLoginManager implements LoginManager {

  private Provider<Login> login;

  public SimpleLoginManager() {
    this(null);
  }

  public SimpleLoginManager(Login login) {
    setLogin(login);
  }

  public void setLogin(final Login login) {
    setLoginProvider(new Provider<Login>() {
      public Login get() {
        return login;
      }
    });
  }

  @Inject(optional = true)
  public void setLoginProvider(Provider<Login> login) {
    this.login = login;
  }

  public Login getLogin(XRequest request) {
    return login.get();
  }
}
